package com.nuo.modules.student.controller;

import java.util.Arrays;
import java.util.Map;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.nuo.common.excel.po.ExportListPo;
import com.nuo.common.utils.Constant;
import com.nuo.modules.question.entity.QuestionEntity;
import com.nuo.modules.question.service.QuestionService;
import com.nuo.modules.signRecord.service.SignRecordService;
import com.nuo.modules.student.po.LoginPo;
import com.nuo.modules.student.po.PwdPo;
import com.nuo.modules.student.po.ResetPO;
import com.nuo.modules.surveyresult.entity.SurveyResultEntity;
import com.nuo.modules.surveyresult.service.SurveyResultService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.nuo.modules.student.entity.StudentEntity;
import com.nuo.modules.student.service.StudentService;
import com.nuo.common.utils.PageUtils;
import com.nuo.common.utils.R;
import org.springframework.web.multipart.MultipartFile;


/**
 *
 *
 * @author nuo
 * @email 3033616351@qq.com
 * @date 2022-11-04 17:03:52
 */
@RestController
@RequestMapping("student")
public class StudentController {
    @Autowired
    private StudentService studentService;

    @Autowired
    QuestionService questionService;

    @Autowired
    SurveyResultService surveyResultService;

    /**
     * 获取学生信息
     */
    @GetMapping("/detail")
    public R info(@RequestParam String token){
        return studentService.info(token);

    }

    /**
     * 登陆
     */
    @PostMapping("/login")
    public R login(@RequestBody LoginPo po){
        return studentService.login(po);
    }

    /**
     * 修改密码
     */
    @PostMapping("/changePwd")
    public R changePwd(@RequestBody PwdPo po){
        return studentService.changePwd(po);
    }

    /**
     * 重置密码
     */
    @PostMapping("/resetPwd")
    public R resetPwd(@RequestBody ResetPO po){
        StudentEntity student = new StudentEntity();
        student.setId(po.getId());
        student.setPassword(Constant.S_PWD);
        studentService.updateById(student);
        return R.ok();
    }


    /**
     * 导入学生信息
     */
    @PostMapping("/importList")
    public R importList(@RequestParam("file") MultipartFile file){
        return studentService.importList(file);
    }

    /**
     * 导出学生信息
     */
    @PostMapping("/exportList")
    public R exportList(@RequestBody ExportListPo<StudentEntity> po){
        return studentService.exportList(po);
    }

    /**
     * 导出所有学生信息
     */
    @RequestMapping("/exportAll")
    public R exportAll(){
        return studentService.exportAll();
    }


    /**
     * 列表
     */
    @RequestMapping("/list")
        public R list(@RequestParam Map<String, Object> params){
        PageUtils page = studentService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
        public R info(@PathVariable("id") Long id){
		StudentEntity student = studentService.getById(id);
        return R.ok().put("student", student);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
        public R save(@RequestBody StudentEntity student){
		studentService.save(student);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
        public R update(@RequestBody StudentEntity student){
		studentService.updateById(student);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
        public R delete(@RequestBody Long[] ids){
        Long id=ids[0];
        StudentEntity one = studentService.getById(id);

        // 删除相关信息
        questionService.remove(new LambdaQueryWrapper<QuestionEntity>().eq(QuestionEntity::getStuId,one.getStudentId()));
        surveyResultService.remove(new LambdaQueryWrapper<SurveyResultEntity>().eq(SurveyResultEntity::getStuId,one.getStudentId()));
        studentService.removeByIds(Arrays.asList(ids));
        return R.ok();
    }

}
